﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YS.Domain.IoT.Extension;
using YS.K12.Application.IoT.RedisHub;

namespace YS.K12.Core.IoT.Plugin
{
    public class TcpSendingPlugin : PluginBase, ITcpSendingPlugin
    {
        private readonly RedisHubLifetimeManager _redisHub;
        private readonly ITcpService _tcpService;
        public TcpSendingPlugin(RedisHubLifetimeManager redisHub, ITcpService tcpService) {
            _tcpService= tcpService;
            _redisHub = redisHub;
        }
        /// <summary>
        ///当即将发送数据时，调用该方法在适配器之后，接下来即会发送数据。
        /// </summary>
        /// <param name="client"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task OnTcpSending(ITcpSession client, SendingEventArgs e)
        {
            // 本地找不到客户端,发布广播
            //if (!_tcpService.TryGetClient(client.GetClientId(), out _))
            //{
            //    await _redisHub.PublishAsync(client.GetClientId(), e.Memory);
            //}
            //// 本地找不到客户端,发布广播
           // await _redisHub.PublishAsync("2411300002", e.Memory);
            await e.InvokeNext();
        }
    }
}
